package bsuir.magistrate.projects.gui.filters.util;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import bsuir.magistrate.projects.core.BinFile;
import bsuir.magistrate.projects.service.BinFileReaderService;

@Service
public final class FilterUtils {

	@Autowired
	private BinFileReaderService binFileReaderService;

	private static final Logger log = LoggerFactory
			.getLogger(FilterUtils.class);

	public static final String TXT_EXTENSION = "txt";
	public static final String BIN_EXTENSION = "bin";

	public String getExtension(final String fileName) {
		int delimiter = fileName.lastIndexOf(".");
		String extension = fileName.substring(delimiter + 1, fileName.length());
		return extension;
	}

	public void initFileChooser(JFileChooser fileChooser) {
		int response = fileChooser.showOpenDialog(null);
		if (response == JFileChooser.APPROVE_OPTION) {
			File file = fileChooser.getSelectedFile();
			String fileName = file.getName();
			String extension = this.getExtension(fileName);

			if (extension.equals(TXT_EXTENSION)) {
				log.info("File has " + TXT_EXTENSION + " extension");
				processFileWithTxtExtension(file);
			} else if (extension.equals(BIN_EXTENSION)) {
				log.info("File has " + BIN_EXTENSION + " extension");
				prosessFileWithBinExtension(file);
			} else {
				log.info("process file with Wrong file extension");
				processFileWithWrongExtension();
			}
		}
	}

	private static String getParentDirectory(File file) {
		String absolutePath = file.getAbsolutePath();
		String parentDirectory = absolutePath.substring(0,
				absolutePath.lastIndexOf(File.separator));
		return parentDirectory;
	}

	private void processFileWithWrongExtension() {
		JOptionPane.showMessageDialog(new JFrame(),
				"Sygnal Analisis handles files only with " + TXT_EXTENSION
						+ " and " + BIN_EXTENSION + " extensions",
				"Wrong file extension", JOptionPane.ERROR_MESSAGE);
	}

	private void prosessFileWithBinExtension(File file) {
		try {
			BinFile binFile = binFileReaderService.getBinFileProperties(file);
			JOptionPane.showMessageDialog(new JFrame(), binFile.toString(),
					"File parameters", JOptionPane.INFORMATION_MESSAGE);
			return;
		} catch (IOException e) {
			log.error(e.toString());
		}
	}

	private void processFileWithTxtExtension(File file) {
		List<String> binFiles = getBinFilesFromTxt(file);
		StringBuilder stringBuilder = new StringBuilder();
		
		for (String locaton : binFiles){
		    stringBuilder.append(locaton + "\n");
		}
		
		JOptionPane.showMessageDialog(new JFrame(), stringBuilder.toString(),
                "Bin Files Locations", JOptionPane.INFORMATION_MESSAGE);
	}

	private static List<String> getBinFilesFromTxt(File file) {
		ArrayList<String> binFiles = new ArrayList<String>();
		String parentDirectory = getParentDirectory(file);
		try {
			FileInputStream inputStream = new FileInputStream(file);
			DataInputStream dataInputStream = new DataInputStream(inputStream);
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(dataInputStream));

			String line;

			while ((line = bufferedReader.readLine()) != null) {
				binFiles.add(parentDirectory + "\\" + line);
				System.out.println(parentDirectory + "\\" + line);
			}

			dataInputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (binFiles.isEmpty()) {
			return null;
		} else {
			binFiles.trimToSize();
			return binFiles;
		}
	}
}
